/*
 * @Author: your name
 * @Date: 2021-05-28 10:33:06
 * @LastEditTime: 2021-05-28 10:41:38
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue3-template\src\hooks\useLoadMore.ts
 */
import {ref, computed, ComputedRef} from 'vue'

import { useStore } from 'vuex'

export interface LoadParams {
    columnId?: string;
    currentPage: number;
    pageSize: number;
}

const useLoadMore = (actionName: string, total:ComputedRef<number>, params:LoadParams={currentPage:1, pageSize:5}) => {
    const store = useStore()
    const { currentPage: current, pageSize, columnId } = params
    const currentPage = ref(current)

    const requestParams = computed(() => ({
        currentPage: currentPage.value,
        pageSize: pageSize,
        columnId: columnId
      }))

      const loadMorePage = () => {
        store.dispatch(actionName, requestParams.value).then(() => {
          currentPage.value++
        })
      }

      const isLastPage = computed(() => {
        return Math.ceil(total.value / pageSize) < currentPage.value
      })
      return {
        loadMorePage,
        isLastPage,
        currentPage
      }

}

export default useLoadMore

